﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FbKPI.Facade;
using FbKPI.Model;

namespace FBKPI.Forms
{
    public partial class EmployeeForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int ID = Convert.ToInt16(Request.QueryString["ID"]);
                if (ID != 0)
                {
                    EmployeeFacade ef = new EmployeeFacade();
                    Employee Employees = ef.GetEmployeeByID(ID);

                    if (Employees != null)
                    {
                        txt_id.Text = Employees.ID.ToString();
                        hfId.Value = Employees.ID.ToString();
                        txt_firstname.Text = Employees.FirstName;
                        txt_lastname.Text = Employees.LastName;
                        txt_department.Text = Employees.Department==null ? string.Empty : Employees.Department.Description;
                        txt_departmentid.Value = Employees.Department == null ? string.Empty : Employees.Department.ID.ToString();
                        txt_section.Text = Employees.Section == null? string.Empty : Employees.Section.Description;
                        txt_sectionid.Value = Employees.Section == null ? string.Empty : Employees.Section.ID.ToString();
                        txt_subsection.Text = Employees.SubSection == null ? string.Empty : Employees.SubSection.Description;
                        txt_subsectionid.Value = Employees.SubSection == null ? string.Empty : Employees.SubSection.ID.ToString();
                        //txt_superior.Text = Employees.Supervisor == null ? string.Empty : Employees.Supervisor.FirstName + " " + Employees.Supervisor.LastName;
                        Employee objSpv = ef.GetEmployeeByID(Employees.Supervisor);
                        if (objSpv != null)
                        {
                            txt_superior.Text = objSpv.FirstName + " " + objSpv.LastName;
                        }
                        //txt_superiorid.Value = Employees.Supervisor == null ? string.Empty : Employees.Supervisor.ID.ToString();
                        txt_superiorid.Value = Employees.Supervisor.ToString();// Employees.Supervisor == null ? string.Empty : Employees.Supervisor.ID.ToString();
                        txt_privilege.Text = Employees.Privilege == null ? string.Empty : Employees.Privilege.Description;
                        txt_privilegeid.Value = Employees.Privilege == null ? string.Empty : Employees.Privilege.ID.ToString();
                        txt_jobposition.Text = Employees.JobPosition==null ?string.Empty : Employees.JobPosition.Position;
                        txt_jobpositionid.Value = Employees.JobPosition==null ? string.Empty : Employees.JobPosition.ID.ToString();
                        txt_jobtitle.Text = Employees.JobTitle;
                        txt_email.Text = Employees.Email;
                        txt_subbranch.Text = Employees.SubBranch==null ? string.Empty :  Employees.SubBranch.Description;
                        txt_subbranchid.Value = Employees.SubBranch==null? string.Empty : Employees.SubBranch.ID.ToString();
                        txt_phone.Text = Employees.Phone;
                        txt_ext.Text = Employees.Ext;
                        chk_admin.Checked = Employees.isAdmin;
                        chk_active.Checked = Employees.isActive;
                        txt_joindate.Text = Employees.JoinDate == null ? "" : Convert.ToDateTime(Employees.JoinDate).ToShortDateString() ;
                        //txt_lastmodifieddate.Text = Employees.LastModifiedDate == null ? "" : Convert.ToDateTime(Employees.LastModifiedDate).ToShortDateString();
                        //txt_lastmodifiedby.Text = Employees.LastModifiedBy;
                        //txt_createdate.Text = Employees.CreateDate == null ? "" : Convert.ToDateTime(Employees.CreateDate).ToShortDateString();
                        //txt_createby.Text = Employees.CreateBy;
                        chk_lock.Checked = Employees.isLock;
                        txtNIK.Text = Employees.NIK;
                    }
                }
                else
                {
                    txt_id.Text = "0";
                }

                if (txt_id.Text == "0") btn_reset.Visible = false;
            }
        }
    }
}